package com.icss.train_sys_back.service.train_manage.impl;

import com.icss.train_sys_back.entity.ticket_manage.TicketNumberEntity;
import com.icss.train_sys_back.entity.train_manage.StationEntity;
import com.icss.train_sys_back.entity.train_manage.TrainNumberEntity;
import com.icss.train_sys_back.entity.train_manage.TrainStationEntity;
import com.icss.train_sys_back.mapper.train_manage.TrainNumberMapper;
import com.icss.train_sys_back.service.train_manage.TrainNumberService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author wk
 * @desc
 * @date 2025/9/25 16:46
 */
@Service("trainNumberService")
public class TrainNumberServiceImpl implements TrainNumberService {
    @Autowired
    private TrainNumberMapper trainNumberMapper;
//    @Autowired
//    private TrainNumberRepository trainNumberRepository;

    @Override
    public List<TrainNumberEntity> findTrainNumberList(String trainNumber, Integer status) {
        return trainNumberMapper.findTrainNumberList(trainNumber, status);
    }

    @Override
    public boolean updateTrainNumber(TrainNumberEntity trainNumber) {
        boolean result = trainNumberMapper.updateTrainNumber(trainNumber);
        // 如果数据库更新成功，同步更新ES
        if (result) {
            // 获取完整的车次信息，包括经停站
            TrainNumberEntity updatedEntity = trainNumberMapper.findTrainNumberByTrainId(trainNumber.getId());
            List<TrainStationEntity> stations = trainNumberMapper.findTrainStationsByTrainNumberId(trainNumber.getId());
            updatedEntity.setTrainStationEntities(stations);
        }
        return result;
    }

    @Override
    public boolean addTrainNumber(TrainNumberEntity trainNumber) {
        return trainNumberMapper.addTrainNumber(trainNumber);
    }

    @Override
    public boolean deleteTrainNumber(Integer id) {
        //根据id删除车次对应的经停站表信息
        trainNumberMapper.deleteTrainStationByTrainNumberId(id);
        return trainNumberMapper.deleteTrainNumber(id);
    }

    //查找车站列表(下拉框显示)
    @Override
    public List<StationEntity> findStationList() {
        return trainNumberMapper.findStationList();
    }

    //根据车次Id获取车次实体，删除车次经停站表需要车次中的车辆号train Number
    @Override
    public TrainNumberEntity findTrainNumberByTrainId(Integer id) {
        return trainNumberMapper.findTrainNumberByTrainId(id);
    }

    //根据trainNumber删除经停站表的所有数据
    @Override
    public boolean deleteArriveStationByTrainNumber(String trainNumber) {
        return trainNumberMapper.deleteArriveStationByTrainNumber(trainNumber);
    }

    //根据车站名获取到车站的经纬度信息  -- X
    @Override
    public Integer findStationXByStationName(String stationName) {
        return trainNumberMapper.findStationXByStationName(stationName);
    }

    //根据车站名获取到车站的经纬度信息  -- Y
    @Override
    public Integer findStationYByStationName(String stationName) {
        return trainNumberMapper.findStationYByStationName(stationName);
    }

    //2.2 根据车次的始发站名称获取该车次的始发站 -- 获取始发站的经纬度用
    @Override
    public StationEntity findStartStationByStartStationName(String startStation) {
        return trainNumberMapper.findStartStationByStartStationName(startStation);
    }

    //经停站插入经停站表
    @Override
    public void insertTrainStation(TrainStationEntity trainStation) {
        trainNumberMapper.insertTrainStation(trainStation);
    }

    //2.4 更新trainNumber表中 经停站数 字段 （传回来的经停站集合的长度）
    @Override
    public void updateTrainNumberStayStationNumber(int size, Integer id) {
        trainNumberMapper.updateTrainNumberStayStationNumber(size, id);
    }

    //根据车次Id获取经停站列表
    @Override
    public List<TrainStationEntity> findTrainStationsByTrainNumberId(Integer trainNumberId) {
        return trainNumberMapper.findTrainStationsByTrainNumberId(trainNumberId);
    }

    //根据车次Id获取票数信息
    @Override
    public TicketNumberEntity getTicketNumberById(Integer trainNumberId, Integer carriageType) {
        TrainNumberEntity trainNumber = trainNumberMapper.findTrainNumberByTrainId(trainNumberId);
        return trainNumberMapper.getTicketNumberByTrainNumber(trainNumber.getTrainNumber(),carriageType);
    }

    //车次添加脚本
    @Override
    public void batchAdd(List<TrainNumberEntity> trainNumberEntityList) {
        trainNumberMapper.batchAdd(trainNumberEntityList);
    }


}
